package com.winning.sx.microframework.controller.sysmanage;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.sx.microframework.common.util.EncryptionUtil;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.manage.UserManageModel;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.service.sysmanage.UserManageService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gongzq
 * @since 2018-03-29
 */
@Controller
@RequestMapping("/base/pmUsers")
public class UsersController {
    @Autowired
    UserManageService userManageService;

    @ResponseBody
    @RequestMapping("/updatePassword")
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();

        Map<String, Object> map = null;
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
                String oldPassword = ((LoginSession) subject.getSession().getAttribute("loginSession")).getcPassword();


                if(!oldPassword.equals(EncryptionUtil.getHash(map.get("oldPass").toString()))){
                    throw new Exception("旧密码错误");
                }

                UserManageModel userModel=new UserManageModel();
                userModel.setcPassword(map.get("cPass").toString());
                userManageService.editUser("updatepassword",userModel);

                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
}

